package cn.emailChat.ai.service.util;

import jakarta.mail.Folder;
import jakarta.mail.Store;

/**
 * @Description IMAP/Jakarta Mail 的通用关闭与安全调用工具
 * @Author susu
 * @Date 2025/8/18
 */
public final class MailIoUtils {
    private MailIoUtils() {}

    public static void closeQuietly(Folder f) {
        if (f != null && f.isOpen()) {
            try { f.close(false); } catch (Exception ignored) {}
        }
    }

    public static void closeQuietly(Store s) {
        if (s != null) {
            try { s.close(); } catch (Exception ignored) {}
        }
    }

    public static <T> T safe(SupplierEx<T> s) {
        try { return s.get(); } catch (Exception e) { return null; }
    }

    @FunctionalInterface
    public interface SupplierEx<T> {
        T get() throws Exception;
    }
}
